package com.btpj.module_main.ui.home

import androidx.lifecycle.Observer
import androidx.paging.PageKeyedDataSource
import com.btpj.jetpack.base.BaseFragment
import com.btpj.jetpack.entity.DataBean
import com.btpj.module_main.R
import com.btpj.module_main.adapter.HomeListAdapter
import com.btpj.module_main.adapter.MyBannerAdapter
import com.btpj.module_main.data.entity.Banner
import com.btpj.module_main.databinding.FragmentHomeBinding
import kotlinx.android.synthetic.main.fragment_home.*

/**
 *  首页
 *
 * @author LTP 2020/7/10
 */
class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>() {

    private val mBannerList = ArrayList<Banner>()
    private val mBannerAdapter by lazy { MyBannerAdapter(mBannerList) }

    companion object {

        @JvmStatic
        fun newInstance() = HomeFragment()
    }

    override fun getLayoutId() = R.layout.fragment_home

    override fun initView() {
        banner.apply {
            addBannerLifecycleObserver(viewLifecycleOwner)
            adapter = mBannerAdapter
        }

        val supportFragmentManager = activity?.supportFragmentManager
        val beginTransaction = supportFragmentManager?.beginTransaction()
        beginTransaction?.replace(R.id.fl_container, HomeListFragment::class.java, null)
        beginTransaction?.commit()
    }

    override fun createObserve() {
        super.createObserve()
        mViewModel.bannerLiveData.observe(viewLifecycleOwner, Observer {
            mBannerList.apply {
                clear()
                addAll(it)
            }
            mBannerAdapter.notifyDataSetChanged()
        })
    }
}